#include "lib.h"
void *memcpy(void *dest, const void *src, size_t count)
{
    if(dest == src) return dest;
    char *pDest = (char *)(dest);
    const char *pSrc = (const char *)(src);

    //目的地址和源地址重叠，从源地址的末尾方向开始拷贝
    if (pDest > pSrc && pDest < pSrc + count)
    {
        pDest = pDest + count - 1;
        pSrc = pSrc + count - 1;
        while (count--)
        {
            *pDest-- = *pSrc--;
        }
    }
    //目的地址和源地址不重叠，从源地址的开始方向拷贝
    else
    {
        while (count--)
        {
            *pDest++ = *pSrc++;
        }
    }

    return pDest;
    return dest;
}

void *memset(void *s, unsigned char c, size_t n)
{
    if(s == NULL) return NULL;
    unsigned char *p = (unsigned char *)s;
    while (n > 0)
    {
        *p = c;
        ++p;
        --n;
    }
    return s;
}

size_t strlen(void* str)
{
    if(str == NULL) return 0;
    unsigned char*dst = (unsigned char*)str;
    while (*dst != 0)
    {
        ++dst;
    }
    return dst - (unsigned char*)str;
}

int memcmp(const void *s1, const void *s2, size_t len)
{
    if(len == 0) return 0;
    unsigned char* p1 = (unsigned char*)s1;
    unsigned char* p2 = (unsigned char*)s2;
    while(len--)
    {
        if(*p1 == *p2)
        {
            ++p1;
            ++p2;
        }
        else if(*p1 > *p2) return 1;
        else return -1;
    }
    return 0;
}